﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class Branches : DetailView<branch>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<branch> EntityList;
        private new branch Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<branch>

        public override List<branch> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.branches.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<branch> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.branches.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override branch GetEntity()
        {
            _context = new project5188Entities();
            Entity = new branch();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTobranches(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<branch> GetEntityListByParameters(string branchName, long workingAreaId, long policeForceAreaId)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (!string.IsNullOrEmpty(branchName) && !string.IsNullOrWhiteSpace(branchName))
            {
                dynamicString += " And it.b_BranchName = @BranchName";
                parameterList.Add(new ObjectParameter("BranchName", branchName));
            }

            if (workingAreaId > 0)
            {
                dynamicString += " And it.za_WorkingTypeParameterValueID_2 = @WorkingAreaId";
                parameterList.Add(new ObjectParameter("WorkingAreaId", workingAreaId));
            }

            if (policeForceAreaId > 0)
            {
                dynamicString += " And it.zb_PoliceForceAreaParameterValueID_3 = @PoliceForceAreaId";
                parameterList.Add(new ObjectParameter("PoliceForceAreaId", policeForceAreaId));
            }

            EntityList = _context.branches.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
